using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Globalization;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
namespace SS
{
	internal class Regis
	{
		private Func objFunc = new Func();
		public Regis()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public bool CheckForRes()
		{
			bool result = false;
			string cDKey = this.GetCDKey(this.InsertInfo("Win32_Processor"));
			if (cDKey.ToUpper() == this.GetSerial().ToUpper())
			{
				result = true;
			}
			return result;
		}
		public int GetCountBill1()
		{
			int result = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT COUNT(ID) AS TOTALITEM FROM [TabBill]";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = int.Parse(sqlDataReader["TOTALITEM"].ToString());
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public int GetCountBill2()
		{
			int result = 0;
			SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
			sqlConnection.Open();
			SqlCommand sqlCommand = sqlConnection.CreateCommand();
			sqlCommand.CommandText = "SELECT COUNT(*) AS TOTALITEM FROM [TabInOutBill]";
			SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
			while (sqlDataReader.Read())
			{
				result = int.Parse(sqlDataReader["TOTALITEM"].ToString());
			}
			sqlDataReader.Close();
			sqlDataReader.Dispose();
			sqlConnection.Close();
			sqlConnection.Dispose();
			return result;
		}
		public string GetCDKey(string Source)
		{
			string text = "";
			string[] array = new string[]
			{
				"M",
				"I",
				"N",
				"H",
				"Q",
				"U",
				"A",
				"N",
				"G",
				"P",
				"L",
				"K",
				"J",
				"H",
				"G",
				"F",
				"D",
				"S",
				"A",
				"Z",
				"X",
				"C",
				"V",
				"B",
				"N",
				"M"
			};
			string[] array2 = new string[]
			{
				"M",
				"I",
				"N",
				"H",
				"Q",
				"U",
				"A",
				"N",
				"G",
				"P",
				"O",
				"H",
				"L",
				"K",
				"N",
				"A",
				"S",
				"D",
				"F",
				"C",
				"V",
				"Z",
				"X",
				"M",
				"J",
				"B"
			};
			if (Source != "")
			{
				text = this.CryptographyMD5(Source);
				char[] array3 = text.ToCharArray(0, text.Trim().Length);
				for (int i = 0; i < array3.Length; i++)
				{
					if (i % 2 == 1)
					{
						text += array3[i];
					}
				}
				char[] array4 = text.Trim().ToCharArray(0, text.Trim().Length);
				text = "";
				for (int j = 0; j < 20; j++)
				{
					if (j % 2 == 1)
					{
						text += array[int.Parse(array3[j].ToString())];
					}
					else
					{
						text += array2[int.Parse(array3[j].ToString())];
					}
				}
			}
			return text;
		}
		public string GetSerial()
		{
			return ConfigurationSettings.AppSettings["License"].ToString();
		}
		public string InsertInfo(string Key)
		{
			string result = "";
            //ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher("select * from " + Key);
            //try
            //{
            //    using (ManagementObjectCollection.ManagementObjectEnumerator enumerator = managementObjectSearcher.Get().GetEnumerator())
            //    {
            //        while (enumerator.MoveNext())
            //        {
            //            ManagementObject managementObject = (ManagementObject)enumerator.get_Current();
            //            PropertyDataCollection.PropertyDataEnumerator enumerator2 = managementObject.get_Properties().GetEnumerator();
            //            try
            //            {
            //                while (enumerator2.MoveNext())
            //                {
            //                    PropertyData current = enumerator2.get_Current();
            //                    if (current.get_Name().ToUpper() == "PROCESSORID")
            //                    {
            //                        if (current.get_Value() != null && current.get_Value().ToString() != "")
            //                        {
            //                            string text = current.get_Value().GetType().ToString();
            //                            if (text == null)
            //                            {
            //                                goto IL_180;
            //                            }
            //                            if (!(text == "System.String[]"))
            //                            {
            //                                if (!(text == "System.UInt16[]"))
            //                                {
            //                                    goto IL_180;
            //                                }
            //                                ushort[] array = (ushort[])current.get_Value();
            //                                string text2 = "";
            //                                ushort[] array2 = array;
            //                                for (int i = 0; i < array2.Length; i++)
            //                                {
            //                                    ushort num = array2[i];
            //                                    text2 = text2 + num.ToString() + " ";
            //                                }
            //                                result = text2;
            //                            }
            //                            else
            //                            {
            //                                string[] array3 = (string[])current.get_Value();
            //                                string text3 = "";
            //                                string[] array4 = array3;
            //                                for (int i = 0; i < array4.Length; i++)
            //                                {
            //                                    string str = array4[i];
            //                                    text3 = text3 + str + " ";
            //                                }
            //                                result = text3;
            //                            }
            //                            continue;
            //                            IL_180:
            //                            result = current.get_Value().ToString();
            //                        }
            //                    }
            //                }
            //            }
            //            finally
            //            {
            //                IDisposable disposable = enumerator2 as IDisposable;
            //                if (disposable != null)
            //                {
            //                    disposable.Dispose();
            //                }
            //            }
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("can't get data because of the followeing error \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //}
			return result;
		}
		public string CryptographyMD5(string source)
		{
			string text = "";
			try
			{
				MD5CryptoServiceProvider mD5CryptoServiceProvider = new MD5CryptoServiceProvider();
				byte[] bytes = Encoding.UTF8.GetBytes(source);
				byte[] array = mD5CryptoServiceProvider.ComputeHash(bytes);
				byte[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					byte b = array2[i];
					text += int.Parse(b.ToString(), NumberStyles.HexNumber).ToString();
				}
			}
			catch
			{
			}
			return text;
		}
	}
}
